package cn.com.fuhuitong.main.home.ui.activity

import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.base.ViewModeActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.vlayout.layout.GridLayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.home.adapter.CaseTabAdapter
import cn.com.fuhuitong.main.home.ui.fragment.HomeCaseFragment
import cn.com.fuhuitong.main.home.vm.CaseViewModel
import com.blankj.utilcode.util.LogUtils
import kotlinx.android.synthetic.main.activity_home_case.*

@Route(path = ARouteConstance.ACTIVITY_URL_CASE)
class HomeCaseActivity : ViewModeActivity<CaseViewModel>() {

    private val charityTabData = mutableListOf("视频素材", "图文素材")

    private lateinit var caseTabAdapter: CaseTabAdapter

    override fun viewModel(): CaseViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_home_case

    override fun initView() {

        image_case_back.setOnClickListener {
            this.finish()
        }

        this.initViewRecyclerAdapter(
            recycler_case_tab, arrayOf(ItemViewType.CASE_TAB)
        )

        text_case_title.text = intent?.getStringExtra(AppConstance.BUNDLE_CASE_TITLE)

        delegateAdapter.addAdapter(
            CaseTabAdapter(
                applicationContext, GridLayoutHelper(charityTabData.size), charityTabData
            ).apply {
                caseTabAdapter = this
            }
        )

        view_pager.adapter = object : FragmentStatePagerAdapter(
            supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getItem(position: Int): Fragment =
                HomeCaseFragment.newInstance(
                    intent?.getIntExtra(AppConstance.BUNDLE_CASE_ACTION, 0) ?: 0, position + 1
                )

            override fun getCount(): Int = charityTabData.size

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                //super.destroyItem(container, position, `object`)
            }
        }

        view_pager.setScrollAnim(false)
        view_pager.setNoScroll(false)

        view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) = Unit

            override fun onPageScrolled(
                position: Int, positionOffset: Float, positionOffsetPixels: Int
            ) = Unit

            override fun onPageSelected(position: Int) {
                caseTabAdapter.selectedPosition = position
            }
        })

        caseTabAdapter.setOnItemClickListener { _, i, _ ->
            caseTabAdapter.selectedPosition = i
            view_pager.currentItem = i
        }

        LogUtils.e("===============${intent?.getIntExtra(AppConstance.BUNDLE_CASE_ACTION, 0)}")
    }

    override fun initViewMode() {

    }


}